import { toRaw } from "vue";

export default {
    /**ccColor转hex */
    toHexColor(ccColor: any): string {
        const hexA = ccColor.a.toString(16);
        return `#${ccColor.toHEX()}${hexA.length === 1 ? "0" + hexA : hexA}`;
    },

    /**hex转ccColor */
    toCCColor(hex: string): any{
        return new cc.Color().fromHEX(hex)
    },

    /**打印object */
    toConsole(target: any){
        console.log(toRaw(target))
    },

    /**对象名 */
    targetName(target: any): string{
        return target?.name
    },

    /**例举出CC事件 */
    toEvents(events: any): any[]{
        const list = events || []
        const tmp = []
        for(let i=0; i<list.length; i++){
            const ev = list[i]
            const target = ev.target?.name
            const component = ev._componentName
            const handler = ev.handler
            const customEventData = ev.customEventData
            tmp.push({target, component, handler, customEventData})
        }
        return tmp
    },

    /**例举出CC对象 */
    toObjects(list: any){
        if(!list) return [];
        const tmp = []
        for(let i=0; i<list.length; i++){
            tmp.push(list[i]?.name)
        }
        return tmp
    },

    /**获取enums */
    getEnum(data: Record<string, number>){
        const list = []
        for(let k in data){
            if(!isNaN(data[k])){
                list.push({ name: k, value: data[k] })
            }
        }
        return list
    }
}